<?php
	
class person	
{
    
    public $personFields = array(	
        "person_number" => "",
        "person_id" => "",
        "firstname" => "",
        "lastname" => "",
        "email" => "",
        "city" => "",
        "address" => "",
        "telephone" => "",
        "fax" => "",
        "cellphone" => "",
        "sidx" => "manuy_number",
        "sord" => "asc",
        "rows" => "20",
        "page" => "1"
    );
    
    
    

					
 	public $personFieldNames = array("person_number","firstname","lastname","email","city","address","telephone","fax","cellphone");								
	
        function __construct() 
        { 
            foreach($this->personFields as $fieldName => $value) 
                $this->personFields[(string)$fieldName]=( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : $this->personFields[(string)$fieldName]);	

        } 
        
        
        
        public function  viewPersons()
	{
		
		   $filter_query_limit="SELECT * FROM person WHERE ";   
                   $filter_query="SELECT COUNT(*) AS NumberOfRecords FROM person WHERE  ";  
                   
                   $filter_query_body = "person_number LIKE '%".$this->personFields["person_number"]."%' AND ";
                   if ($this->personFields["person_id"]=="")
                       $filter_query_body .= "person_id LIKE '%".$this->personFields["person_id"]."%' AND ";
                   else
                   $filter_query_body .= "person_id = '".$this->personFields["person_id"]."' AND ";
                   $filter_query_body .= "firstname LIKE '%".$this->personFields["firstname"]."%' AND ";
                   $filter_query_body .= "lastname LIKE '%".$this->personFields["lastname"]."%' AND ";
                   $filter_query_body .= "email LIKE '%".$this->personFields["email"]."%' AND ";
                   $filter_query_body .= "city LIKE '%".$this->personFields["city"]."%' AND ";
                   $filter_query_body .= "address LIKE '%".$this->personFields["address"]."%' AND ";
                   $filter_query_body .= "telephone LIKE '%".$this->personFields["telephone"]."%' AND ";
                   $filter_query_body .= "fax LIKE '%".$this->personFields["fax"]."%' AND ";
                   $filter_query_body .= "cellphone LIKE '%".$this->personFields["cellphone"]."%' ";

                   $filter_query_body .= "ORDER BY " .$this->personFields["sidx"]." ".$this->personFields["sord"]  ;
                   
                   $filter_query .=$filter_query_body;
                       
                   $filter_query_limit .=$filter_query_body. " LIMIT " .(($this->personFields["page"]-1)*$this->personFields["rows"])." , " .(($this->personFields["page"])*$this->personFields["rows"]);

                   //echo $filter_query_limit;


                   #----- get data from DB ------#
                   
                   require("../includes/openConnection.php"); 
                   $filter_query_result = mysql_query($filter_query) or die(mysql_error());
                   $filter_query_limit_result = mysql_query($filter_query_limit) or die(mysql_error());
                   require("../includes/closeConnection.php");
                   #----- get data from DB ------#
		
                
                   #----- return json ------#
                   $jsonData = array();
                   $rows = array();
                   while($r1 = mysql_fetch_assoc($filter_query_limit_result)) 
                            $rows[] = $r1;


                   $r2 = mysql_fetch_assoc($filter_query_result);
                   $jsonData['records'] = $r2['NumberOfRecords'];
                   $jsonData['total']= ceil($jsonData['records']/$this->personFields["rows"]);
                   $jsonData['page']=$this->personFields["page"];
                   $jsonData['rows']=$rows;

                   print json_encode($jsonData);
		
	}
	
	public function  editPerson()
	{
            
                #----- insert data to DB ------#
		require("../includes/openConnection.php"); 
                if(
		mysql_query("UPDATE person SET  `person_id` ='".$this->personFields["person_id"]."',
                `firstname` ='".$this->personFields["firstname"]."',
                `lastname` ='".$this->personFields["lastname"]."',
                `email` ='".$this->personFields["email"]."',
                `city` ='".$this->personFields["city"]."',
                `address` ='".$this->personFields["address"]."',
                `telephone` ='".$this->personFields["telephone"]."',
                `fax` ='".$this->personFields["fax"]."',
                `cellphone` ='".$this->personFields["cellphone"]."' WHERE  person_number ='".$this->personFields["person_number"]."';")  
                ) echo "ok"; else  echo mysql_error();
          
		$person_number=mysql_insert_id();
		require("../includes/closeConnection.php");
		#----- insert data to DB ------#	
		return $person_number;
		
	}
	
	public function  addPerson()
	{
		#----- insert data to DB ------#
		require("../includes/openConnection.php"); 
                if(
		mysql_query("INSERT INTO  person (
                `person_number` ,
                `person_id` ,
                `firstname` ,
                `lastname` ,
                `email` ,
                `city` ,
                `address` ,
                `telephone` ,
                `fax` ,
                `cellphone`
                )
                VALUES (
                NULL ,'".$this->personFields["person_id"]."','".$this->personFields["firstname"]."','".$this->personFields["lastname"]."','".$this->personFields["email"]."','".$this->personFields["city"]."','".$this->personFields["address"]."','".$this->personFields["telephone"]."','".$this->personFields["fax"]."','".$this->personFields["cellphone"]."');")
                ) 
                    echo "ok"; 
                else  
                    echo mysql_error();
                
               
                
                $filter_query_result = mysql_query("select max(person_number) as lastPerson from person") or die(mysql_error());
                $r2 = mysql_fetch_assoc($filter_query_result);
               
                
                
		$person_number= $r2['lastPerson'];
		require("../includes/closeConnection.php");
		#----- insert data to DB ------#	
		return $person_number;
	}
	
	public function  deletePerson()
	{
		#----- delete data to DB ------#
		
		require("../includes/openConnection.php");  
		if(mysql_query("DELETE FROM person WHERE person_number='".$this->personFields["person_number"]."';")) echo "ok";
		require("../includes/closeConnection.php");
		#----- delete data to DB ------#	
			
	}
        public function  personExsit()
	{
		require("../includes/openConnection.php");  
               // echo "SELECT person_id FROM person WHERE person_id='".$this->personFields["person_id"]."';";
                $result =mysql_query("SELECT person_id FROM person WHERE person_id='".$this->personFields["person_id"]."';");
                if(mysql_num_rows($result) > 0)
		{
                    $row = mysql_fetch_array($result);
                    echo $row['person_id'];
                }
                else
                {
                   echo "false";  
                }
		require("../includes/closeConnection.php");
					
	}

}




?>